﻿using System;
using Silver3D.Math;

namespace Silver3D.Graphics
{
    public class Vertex
    {
        /// <summary>
        /// A pointer to the VertexInfo structure that represents this vertex layout.
        /// </summary>
        public VertexInfo info { get; private set; }

        /// <summary>
        /// The position for this vertex.
        /// </summary>
        public Vec3 position { get; private set; }

        /// <summary>
        /// The normal vector for this vertex.
        /// </summary>
        public Vec3 normal { get; private set; }

        /// <summary>
        /// The texture coordinate vector for this vertex.
        /// </summary>
        public Vec4 texCoord { get; private set; }

        /// <summary>
        /// The color vector for this vertex.
        /// </summary>
        public Vec4 color { get; private set; }

        public Vertex(VertexInfo info, double[] data, int offset)
        {
            this.info = info;
            double a, b, c, d;

            a = data[offset++];
            b = data[offset++];
            c = data[offset++];
            if (info.PositionSize > 3) d = data[offset++];
            else d = 1.0;

            position = new Vec4(a, b, c, d).ToVec3();

            if (info.HasNormal)
            {
                a = data[offset++];
                b = data[offset++];
                c = data[offset++];
                normal = new Vec3(a, b, c);
            }

            if (info.HasTexCoord)
            {
                a = data[offset++];
                b = data[offset++];
                c = d = 0.0;
                if (info.TexCoordSize > 2) c = data[offset++];
                if (info.TexCoordSize > 3) d = data[offset++];
                texCoord = new Vec4(a, b, c, d);
            }

            if (info.HasColor)
            {
                a = data[offset++];
                b = data[offset++];
                c = data[offset++];
                if (info.ColorSize > 3) d = data[offset++];
                else d = 1.0;
                color = new Vec4(a, b, c, d);
            }
        }
    }
}
